package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.TalkbackTerminalsModelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

import java.util.Objects;
import java.util.Optional;

/**
 * Created by huanghaosheng on 2021/4/12 21:41
 */
@Service
@AttrHandler(TalkbackTerminalsModelAttrHandler.class)
public class TalkbackTerminalsModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (KiteStringUtils.isEqual(attr.getValue(), "110006936")) { //雁飞Cat1-POC-001
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "242", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7600301202", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "100007525")) { //POC020
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "320", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7600300810", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "100007526")) { //POC030
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "525", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7633300371", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "100007527")) { //POC040
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "610", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7600300812", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "100007528")) { //POC050
            // 产品公允价
            operateInstAttrService.setAttrValue(inst, "200004115", "1685", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7600300814", data.getSceneInstId(), null);
        }
        else if (KiteStringUtils.isEqual(attr.getValue(), "100007529")) { //POC060
            // 标准销售价
            operateInstAttrService.setAttrValue(inst, "200004115", "1053", data.getSceneInstId(), null);
            // 物资编码
            operateInstAttrService.setAttrValue(inst, "200004178", "7600300816", data.getSceneInstId(), null);
        }

        String rootInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        KiteOfferInst rootOfferInst = querySceneInstService.getOfferInst(data.getCustId(), rootInstId);
        String cateId = querySceneInstService.getOfferInstAttrValue(data.getCustId(), rootInstId, "cateId");
        if("2004109".equals(cateId) && "30000281".equals(rootOfferInst.getSpecId())){
            linkDealPrice(attr,inst,data);
        }
    }

    private void linkDealPrice(KiteAttr attr, KiteInst inst, AttrData data){
        //公允价
        KiteAttr fairPriceAttr = inst.getAttr("200004115");
        //物资编码
        KiteAttr materialCodeAttr = inst.getAttr("200004178");

        if(Objects.nonNull(fairPriceAttr)){
            String fairPrice = AttrValueCache.getAttrValueByAttrValueName(fairPriceAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst,"200004115", fairPrice, data.getSceneInstId(),null);
        }
        if(Objects.nonNull(materialCodeAttr)){
            String fairPrice = AttrValueCache.getAttrValueByAttrValueName(materialCodeAttr.getAttrId(), attr.getValueDesc());
            operateInstAttrService.setAttrValue(inst,"200004178", fairPrice, data.getSceneInstId(),null);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
